package com.zzy.test.controller;

import com.zzy.test.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * @author Lucas-zhang
 * @description TODO
 * @date 2022-03-20 16:02
 */
@RestController
public class LoginController {

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 获取邮件验证码
     * 这里随机生成
     * @param email
     * @return
     */
    @GetMapping("/getEmailCode")
    public String getEmailCode(String email) {
        // 一些校验逻辑省略.. 邮箱验证等
        String code = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuffer sb = new StringBuffer(4);
        for (int i = 0; i < 4; i++) {
            char c = code.charAt(new Random().nextInt(code.length()));
            sb.append(c);
        }
        System.out.println("code = " + sb.toString());
        // 缓存到redis
        redisUtil.set(email, sb.toString(), 60);

        return sb.toString();
    }
}
